//
//  ForumReplyViewController.m
//  Blank
//
//  Created by 周星宇 on 15/9/25.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ForumReplyViewController.h"
#import "AFNetWorkingGet.h"
#import "ForumReplyModel.h"
#import "ForumReplyTableCell.h"
#import "ForumDetailViewController.h"
#import <MJRefresh.h>
#import "UMSocial.h"

@interface ForumReplyViewController () <UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate>

@property (nonatomic, strong) NSMutableArray *forumReplyArray;
@property (nonatomic, strong) UITableView *tableView;
//@property (nonatomic, strong) NSString *title;

@end

@implementation ForumReplyViewController

@synthesize title;

- (void)dataHandle{
    self.forumReplyArray = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:ForumReplyURL, self.fid];
    [AFNetWorkingGet GETWithAFNByURL:url completion:^(id result) {
        NSArray *subArray = [result objectForKey:@"data"];
        for (NSDictionary *dic in subArray) {
            ForumReplyModel *replyModel = [ForumReplyModel ForumReplyModelWithDic:dic];
            [self.forumReplyArray addObject:replyModel];
        }
        if (self.forumReplyArray.count > 0) {
            [self.tableView reloadData];
        }
    }];
}

- (void)dataHeaderFooter{
    self.tableView.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        NSMutableArray *arr = [NSMutableArray array];
        NSString *url = [NSString stringWithFormat:ForumReplyURL, self.fid];
        [AFNetWorkingGet GETWithAFNByURL:url completion:^(id result) {
            NSArray *subArray = [result objectForKey:@"data"];
            for (NSDictionary *dic in subArray) {
                ForumReplyModel *replyModel = [ForumReplyModel ForumReplyModelWithDic:dic];
                [arr addObject:replyModel];
            }
            if (self.forumReplyArray.count > 0) {
                [self.forumReplyArray removeAllObjects];
                self.forumReplyArray = arr;
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }
        }];
    }];
    __block NSInteger i = 2;
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        NSString *url = [NSString stringWithFormat:ForumReplyUpURL, i, self.fid];
        i++;
        [AFNetWorkingGet GETWithAFNByURL:url completion:^(id result) {
            NSArray *subArray = [result objectForKey:@"data"];
            for (NSDictionary *dic in subArray) {
                ForumReplyModel *replyModel = [ForumReplyModel ForumReplyModelWithDic:dic];
                [self.forumReplyArray addObject:replyModel];
            }
            [self.tableView reloadData];
            [self.tableView.footer endRefreshing];
        }];
    }];
}

- (void)tableViewSubView{
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ForumReplyTableCell class] forCellReuseIdentifier:@"reply"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = [NSString stringWithFormat:@"%@论坛", self.name];
    [AFNetWorkingGet popAlertIfNoInter:self];
    [self withNavigationItem];
    [self dataHandle];
    [self tableViewSubView];
    [self dataHeaderFooter];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNightModel) name:@"nightModelChange" object:nil];
    //    NSString *priorImageName = i == 0 ? self.array.lastObject :  self.array[i - 1];
}

- (void)withNavigationItem{
    UIButton *leftMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftMenuBtn.frame = CGRectMake(0, 0, 25 * Windth, 25 * Height);
    [leftMenuBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [leftMenuBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftMenuBtn];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 25 * Windth, 25 * Height);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100 * Height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.forumReplyArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSString *headerStr = @"最新回复";
    return headerStr;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30 * Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ForumReplyTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reply"];
    ForumReplyModel *replyModel = [self.forumReplyArray objectAtIndex:indexPath.row];
    cell.replyModel = replyModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ForumDetailViewController *forumDetail = [[ForumDetailViewController alloc] init];
    ForumReplyModel *replyModel = [self.forumReplyArray objectAtIndex:indexPath.row];
    forumDetail.tid = replyModel.tid;
    self.title = replyModel.subject;
    
    [self.navigationController pushViewController:forumDetail animated:YES];
}

- (void)share{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:self.title shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina, UMShareToTencent, UMShareToDouban, UMShareToRenren, nil] delegate:self];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) changeNightModel
{
    if ([NightModel getColorModel]) {
        self.tableView.backgroundColor = [UIColor colorWithRed:149 / 255.0 green:149 / 255.0 blue:149 / 255.0 alpha:0.7];
        self.tableView.sectionIndexColor = [UIColor greenColor];
        [self.tableView reloadData];
    }
    else{
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.sectionIndexBackgroundColor = nil;
        self.tableView.sectionIndexColor =nil;
        [self.tableView reloadData];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
